package core;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import core.QuestionPattern.Level;

/**
 * @author Boaz
 *
 */
public class Question {
	private final PQuestionGenerator _generatingGenerator;
	private final int _patternId;
	private final String _question;
	private final int _correctAnswerIndex;
	private final String[] _answers;
	private boolean _questionHasBeenAnswered = false;
	private boolean _userAnsweredCorrectly = false;



	public Question(PQuestionGenerator questionGenerator, int questionId, String question, String correctAnswer,
			String[] wrongAnswers, Random random) {

		_generatingGenerator = questionGenerator;
		_patternId = questionId;
		_question = question;

		// initialize answers array and shuffle it
		int wrongAnswersLength = wrongAnswers.length;
		_answers = new String[wrongAnswersLength + 1];
		System.arraycopy(wrongAnswers, 0, _answers, 0, wrongAnswersLength);
		_answers[wrongAnswersLength] = correctAnswer;
		_correctAnswerIndex = shuffleAnswers(correctAnswer, random);

	}

	/**
	 * shuffles the answers and return the new index of the correct one
	 * 
	 * @param correctAnswer
	 * @param random
	 * @return new index of the correct answer
	 */
	private int shuffleAnswers(String correctAnswer, Random random) {
		int correctAnswerIndex = -1;

		//	Shuffle the answers
		List<String> answersList = Arrays.asList(_answers);
		Collections.shuffle(answersList, random);

		//	Populate answers array and find the index of the correct answer
		for (int i = 0; i < _answers.length; i++) {
			_answers[i] = answersList.get(i);
			if (_answers[i].equals(correctAnswer)) {
				correctAnswerIndex = i;
			}
		}

		return correctAnswerIndex;
	}

	//	get user's choice	-	to be called by GUI
	public void usersChoice(int choice){
		userAnswered(choice == _correctAnswerIndex);
	}
	
	//	update user's answer and statistics at question generator  
	private void userAnswered(final boolean correctly){
		_questionHasBeenAnswered = true;
		_userAnsweredCorrectly = correctly;
		_generatingGenerator.aQuestionHasBeenAnswered(_patternId,correctly);
		
	}

	public String getQuestion() {
		return _question;
	}

	public String[] getAnswers() {
		return _answers;
	}

	public int getCorrectAnswerIndex() {
		return _correctAnswerIndex;
	}

	public int getPatternId() {
		return _patternId;
	}

	public boolean isQuestionHasBeenAnswered() {
		return _questionHasBeenAnswered;
	}

	public boolean isUserAnsweredCorrectly() {
		return _userAnsweredCorrectly;
	}
	
//	This method is called to evaluate the right level of a question pattern based on it's success statistics
	public static Level getLevelOf(double patternStatistics) {
		int successOutOf10 = (int) ((patternStatistics * 100) / 10 );

		switch(successOutOf10){
		case 10:	return Level.ONE;
		case 9:		return Level.ONE;
		case 8:		return Level.TWO;
		case 7:		return Level.THREE;
		case 6:		return Level.FOUR;
		case 5:		return Level.FIVE;
		case 4: 	return Level.SIX;
		case 3:		return Level.SEVEN;
		case 2:		return Level.EIGHT;
		case 1:		return Level.NINE;
		case 0:		return Level.TOP;
		default:	return Level.ONE;
		}
	}


	public static enum Level {
		ONE(1.0, 0.9),
		TWO(0.9, 0.8),
		THREE(0.8, 0.7),
		FOUR(0.7, 0.6),
		FIVE(0.6, 0.5),
		SIX(0.5, 0.4),
		SEVEN(0.4, 0.3),
		EIGHT(0.3, 0.2),
		NINE(0.2, 0.1),
		TOP(0.1, 0.0);

		public final double ceilValue;
		public final double floorValue;
		
		private Level(double cValue, double fValue){
			ceilValue = cValue;
			floorValue = fValue;
		}
	}

	
	
}
